package com.tbit.uqbike.quartz;

import com.tbit.uqbike.constant.ControlTypeConstant;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.object.pojo.AccountConfigExt;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.business.TerControlService;
import com.tbit.uqbike.webmanager.dao.core.AccountConfigExtDao;
import com.tbit.uqbike.webmanager.dao.core.MachineBorrowDao;
import com.tbit.uqbike.webmanager.util.StringUtils;
import com.tbit.uqbike.webmanager.util.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: uqbike-manager-new
 * @description: 低电提醒
 * @author: Jing
 * @create: 2021-05-21 13:50
 **/
@Component
@Slf4j
public class LowBatteryReminderQuartz {

    @Autowired
    private MachineBorrowDao machineBorrowDao;
    @Autowired
    private AccountConfigExtDao accountConfigExtDao;

    @Scheduled(cron = "0 0/3 * * * ?")
    public void lowBatteryDetection() {
        List<Map<String, Object>> machineList = machineBorrowDao.getAllMachine();
        List<AccountConfigExt> socPower = accountConfigExtDao.selectAccountConfigExtList(null, "socPower");
        Map<Integer, String> socPowerMap = new HashMap<>();
        socPower.forEach(s -> socPowerMap.put(s.getAccountId(), s.getParamValue()));
        Map<Integer, Integer> configs = new HashMap<>();
        if (StringUtils.isNotEmpty(machineList)) {
            long startTime = System.currentTimeMillis();
            AtomicInteger i = new AtomicInteger();
            machineList.forEach(map -> {
                if (StringUtils.isNotNull(map)) {
                    Integer accountId = (Integer) map.get("accountId");
                    if (!configs.containsKey(accountId)) {
                        List<AccountConfigExt> accountConfigExts = accountConfigExtDao.selectAccountConfigExtList(accountId, "showLowBattery");
                        /*配置了低电提醒配置进行电量判断来进行语音提醒*/
                        if (StringUtils.isNotEmpty(accountConfigExts)) {
                            int battery = Integer.parseInt(accountConfigExts.get(0).getParamValue());
                            /*保存到map中减少数据库查询*/
                            configs.put(accountId, battery);
                        } else {
                            configs.put(accountId, null);
                        }
                    }
                    final Integer socPercent = configs.get(accountId);
                    Integer socPercentInDb = (Integer) map.get("socPercent");
                    Integer socPowerCount = socPowerMap.get(accountId) == null ? 0 : Integer.parseInt(socPowerMap.get(accountId));
                    if (configs.containsKey(accountId) && StringUtils.isNotNull(socPercent) && socPercentInDb < socPercent && socPercentInDb > socPowerCount) {
                        String userCode = (String) map.get("userCode");
                        TerControlService terControlService = SpringUtils.getBean(TerControlService.class);
                        /*发送低电指令*/
                        terControlService.sendControl(new LoginInfo(UserConstant.USER_BRAND, UserConstant.USER_ADMIN, UserConstant.USER_BRAND),
                                terControlService.getOrderInfo(userCode, ControlTypeConstant.CONTROL_TYPE_VOICE, ControlTypeConstant.CONTROL_TYPE_VOICE_LOWPOWER, "骑行中车辆低电语音播报提醒", null));
                        log.info("车辆" + userCode + "低电语音推送");
                        i.getAndIncrement();
                    }
                }
            });
            if (i.get() > 0) {
                log.info("骑行中低电车辆推送语音耗时：" + (System.currentTimeMillis() - startTime) + "毫秒");
            }
        }
    }
}
